/**
 * 车辆信息初始化
 */
var QRCode = {
    id: "QRCodeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};


QRCode.showErrInfo = function(){
	Feng.error("文件不存在!");
}
/**
 * 初始化表格的列
 */
QRCode.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '二维码编号', field: 'codeNum', align: 'center', valign: 'middle', sortable: true},
        {title: '二维码类型', field: 'codeTypeVal', align: 'center', valign: 'middle', sortable: true},
        {title: '二维码状态', field: 'status', align: 'center', valign: 'middle', sortable: true,
        	formatter : function(value, row,index) {
        		if(value == 1){
        			return "已创建";
        		}else if(value == 2){
        			return "已打印";
        		}else if( value == 3){
        			return "已使用";
        		}
            }
        },
        {title: '关联定损单号', field: 'orderNo', align: 'center', valign: 'middle', sortable: true
        	
        },
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true,
        	formatter : function(value, row,index) {
        		if(value){
        			return value.substring(0,19);
        		}
                
            }
        },
        {title: '打印时间', field: 'printTime', align: 'center', valign: 'middle', sortable: true,
        	formatter : function(value, row,index) {
        		if(value){
        			return value.substring(0,19);
        		}
            }
        },
        {title: '使用时间', field: 'useTime', align: 'center', valign: 'middle', sortable: true,
        	formatter : function(value, row,index) {
        		if(value){
        			return value.substring(0,19);
        		}
            }
        }
        ];
};

/**
 * 检查是否选中
 */
QRCode.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        QRCode.seItem = selected;
        return true;
    }
};

QRCode.singleCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
    	if(selected.length > 1){
    		Feng.info("请选择表格中的一条记录进行操作！");
            return false;
    	}else{
    		QRCode.seItem = selected[0];
            return true;
    	}
        
    }
};

//批量生成二维码
QRCode.openAddQRCodes = function(){
	var index = layer.open({
        type: 2,
        title: '创建二维码',
        area: ['80%', '30%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/base/qrcode/toAdd'
    });
    this.layerIndex = index;
}



QRCode.openCodeView = function(){
	if (this.singleCheck()) {
        var index = layer.open({
            type: 2,
            title: '查看二维码',
            area: ['80%', '80%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/base/qrcode/toView/' + QRCode.seItem.id
        });
        this.layerIndex = index;
    }
}

QRCode.openQRCodePrint = function(){
	if (this.check()) {
		var selectedItems = QRCode.seItem;
    	
    	var ids = "";
    	for(var i=0;i<selectedItems.length;i++){
    		var selectedItem = selectedItems[i];
    		if(i<selectedItems.length - 1){
    			ids = ids+selectedItem.id + "," ;
    		}else{
    			ids = ids+selectedItem.id;
    		}
    	}
		
		var index = layer.open({
            type: 2,
            title: '打印二维码',
            area: ['100%', '100%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/base/qrcode/toPrint?ids=' + ids
        });
        this.layerIndex = index;
    }
}

/**
 * 删除
 */
QRCode.delete = function () {
    if (this.check()) {

    	var selectedItems = QRCode.seItem;
    	
    	var ids = "";
    	var names = "";
    	for(var i=0;i<selectedItems.length;i++){
    		var selectedItem = selectedItems[i];
    		if(i<selectedItems.length - 1){
    			ids = ids+selectedItem.id + "," ;
    			names = names+selectedItem.file_name +",";
    		}else{
    			ids = ids+selectedItem.id;
    			names = names+selectedItem.file_name;
    		}
    	}
    	
    	
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/file/del", function (data) {
            	if(data.resultCode==0){
            		Feng.success("删除成功!");
                    QRCode.table.refresh();
            	}else{
            		Feng.error(data.msg + "!");
            	}
                
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids);
            ajax.start();
        };

        Feng.confirm("是否刪除[" + names + "]?", operation);
    }
};



/**
 * 重置
 */
QRCode.resetSearch = function () {
    $("#condition").val("");
    QRCode.table.refresh();
};

/**
 * 查询字典列表
 */
QRCode.search = function () {
	var queryData = {};
    
    //QRCode.table.refreshOptions({pageNumber:1});
    QRCode.table.refresh({query: queryData});
};

$(function () {
	QRCode.init(); 
});

QRCode.init = function(){
	var defaultColunms = QRCode.initColumn();
	var paramsId =[
		 {
		      "key":"codeNum",
		      "type":"input",
		      "id":"codeNum"
	     },
	     {
	          "key":"codeType",
	          "type":"select",
	          "id":"codeType"
	      },
	      {
	          "key":"status",
	          "type":"select",
	          "id":"status"
	      }
		];
    var table = new BSTable(QRCode.id, "/base/qrcode/list", defaultColunms,paramsId);
    table.setPaginationType("server");
    QRCode.table = table.init();
}

QRCode.close = function () {
    parent.layer.close(window.parent.QRCode.layerIndex);
};


